###### Perspective API for Silenced Text

## Packages
library(pacman)
p_load(foreign,
       tidyverse,
       peRspective,
       usethis)

# usethis::edit_r_environ() # Only need to do once
# Add to .Renvrion editor: perspective_api_key = "insert own key"

## Load Data
df <- read.csv("04-Perspective-API/data/02-clean-data/clean_data.csv", stringsAsFactors = F)
names(df) <- c("id", "ctext")
df$id <- as.character(df$id)

# Test Sample
df_head <- as_tibble(head(df, 100))

# Cleaned data
api_df <- df %>%
  prsp_stream(text = ctext,
              text_id = id,
              score_model = c("TOXICITY"),
              safe_output = T,
              doNotStore = T)

names(api_df) <- c("id", "error", "tox_score")

# Merge back with texts for easy reading
full_df <- merge(api_df, df)

write.csv(full_df, "04-Perspective-API/data/02-clean-data/api_coded_texts.csv")